package com.dragon.www.smarthome.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dragon.www.smarthome.R;
import com.dragon.www.smarthome.activity.MainActivity;
import com.dragon.www.smarthome.entity.Device;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by daiye on 2016/6/1.
 */
public class ConferenceAdapter extends BaseAdapter {
    private List<Device> devices = new ArrayList<>();
    private Context mcontext;
    private DisplayImageOptions defaultOptions;
    private boolean mbedit;
    private HashMap<String, Object> mHasIntoEditModeMap = new HashMap<String, Object>();

    public ConferenceAdapter(Context context) {
        mcontext = context;
        defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                .build(); // 创建配置过得DisplayImageOption对象
    }

    public void setData(List<Device> devs) {
        this.devices.clear();
        for(int i = 0; i < devs.size(); i++){
            this.devices.add(devs.get(i));
        }
        Device device = new Device();
        device.setDeviceName("+");
        device.setDeviceId(-1);
        device.setType("-1");
        this.devices.add(device);
        notifyDataSetChanged();
    }

    public void setDeviceEditEnable(boolean bEdit){
        mbedit = bEdit;
    }

    public void setDeviceEditMap(HashMap<String, Object> map){
        mHasIntoEditModeMap = map;
    }

    @Override
    public int getCount() {
        return devices.size();
    }

    @Override
    public Object getItem(int position) {
        return devices.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Device device = devices.get(position);
        DeviceViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new DeviceViewHolder();
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.conference_device_view_item, parent, false);
            convertView.setLayoutParams(new AbsListView.LayoutParams((int) (parent.getWidth() / 4), (int) (parent.getHeight() / 2)));// 动态设置item的高度
            viewHolder.iv_device = (ImageView) convertView.findViewById(R.id.iv_device);
            viewHolder.iv_device_edit = (ImageView) convertView.findViewById(R.id.iv_device_edit);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (DeviceViewHolder) convertView.getTag();
            convertView.setLayoutParams(new AbsListView.LayoutParams((int) (parent.getWidth() / 4), (int) (parent.getHeight() / 2)));// 动态设置item的高度
        }

        if(mbedit && (position < devices.size()-1)){
            viewHolder.iv_device_edit.setVisibility(View.VISIBLE);
            if(mHasIntoEditModeMap.containsKey(String.valueOf(position))) {
                viewHolder.iv_device_edit.setImageResource(R.drawable.selected);
            }else{
                viewHolder.iv_device_edit.setImageResource(R.drawable.uncheck);
            }
        }else{
            viewHolder.iv_device_edit.setVisibility(View.GONE);
        }

        if(device.getType() == MainActivity.TYPE_LAMP){
            ImageLoader.getInstance().displayImage("drawable://" + R.drawable.lamp,
                    viewHolder.iv_device, defaultOptions);
        }else if(device.getType() == MainActivity.TYPE_AIR){
            ImageLoader.getInstance().displayImage("drawable://" + R.drawable.aircondition,
                    viewHolder.iv_device, defaultOptions);
        }else if(device.getType() == MainActivity.TYPE_CURTAIN){
            ImageLoader.getInstance().displayImage("drawable://" + R.drawable.curtain,
                    viewHolder.iv_device, defaultOptions);
        }else if(device.getType() == MainActivity.TYPE_ENTANCEGUARD){
            ImageLoader.getInstance().displayImage("drawable://" + R.drawable.entrance_guard,
                    viewHolder.iv_device, defaultOptions);
        }else if(device.getType() == MainActivity.TYPE_HUMIDIFIER){
            ImageLoader.getInstance().displayImage("drawable://" + R.drawable.humidifier,
                    viewHolder.iv_device, defaultOptions);
        }else if(device.getType() == MainActivity.TYPE_PLANTWALL){
            ImageLoader.getInstance().displayImage("drawable://" + R.drawable.flower,
                    viewHolder.iv_device, defaultOptions);
        }

        if(position == devices.size()-1){
            ImageLoader.getInstance().displayImage("drawable://" + R.drawable.add,
                    viewHolder.iv_device, defaultOptions);
        }

        return convertView;
    }

    public class DeviceViewHolder {
        public ImageView iv_device;
        public ImageView iv_device_edit;
        public TextView tv_device_name;
    }
}
